﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;

namespace RM.Entities.Data
{
    public class CandidateManager
    {
        public void Save(Candidate candidate)
        {
            using (ISession session = DataManager.SessionFactory.OpenSession())
            {
                session.SaveOrUpdate(candidate);
            }
        }

        public Candidate GetById(int candidateId)
        {
            using (ISession session = DataManager.SessionFactory.OpenSession())
            {
                ICriteria criteria = session.CreateCriteria(typeof(Candidate));
                criteria.Add(Restrictions.Eq("Candidate.Id", candidateId));
                criteria.SetMaxResults(1);
                return (Candidate)criteria.UniqueResult();
            }
        }

        public IList<Candidate> GetAll()
        {
            using (ISession session = DataManager.SessionFactory.OpenSession())
            {
                ICriteria criteria = session.CreateCriteria(typeof(Candidate));
                return criteria.AddOrder(new Order("FirstName", true)).List<Candidate>();
            }
        }

        public IList<Candidate> GetAllBySearch(string searchString)
        {
            using (ISession session = DataManager.SessionFactory.OpenSession())
            {
                ICriteria criteria = session.CreateCriteria(typeof(Candidate));
                criteria.Add(Restrictions.Or(Restrictions.Or(Restrictions.Like("FirstName", searchString, MatchMode.Start)
                    , Restrictions.Like("LastName", searchString, MatchMode.Start))
                    , Restrictions.Like("FullName", searchString, MatchMode.Start)));
                return criteria.AddOrder(new Order("FirstName", true)).List<Candidate>();
            }
        }
    }
}
